package com.ncs.leisure.beans.memory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONObject;

import com.ncs.leisure.beans.ui.vo.Room;
import com.ncs.leisure.beans.ui.vo.RoomList;

public class MapList {
	
	HashMap<Integer,MapMirror> hashMapMirror=null;
	
	//初始化
	
	public MapList(){
		
		if(hashMapMirror==null){
			
			hashMapMirror=new HashMap<Integer,MapMirror>();
			
		}
		
	}
	
	//传入账号列表
	
	public String addMapMirror(MapMirror mapMirror){
				
		hashMapMirror.put(mapMirror.getId(), mapMirror);
		
		return "ok";
		
	}
	
	//移除账号列表

	public String delMapMirror(Integer mirrorId){
		
		return null;
		
	}
	
	//列表集合
	
	public String showHall(int nowPage){
		
		RoomList roomList=new RoomList();
		
		roomList.setTotal(hashMapMirror.size());
		
		roomList.setNowPage(nowPage);
		
		//预设值
		
		roomList.setSize(8);
		
		List<Room> rooms=new ArrayList<Room>();
		
		//*********************************
		//获得指定位置的集合，根据nowPage
		
		//hashMapMirror.values();
		
		Iterator it = hashMapMirror.values().iterator();
		
		int i=0;
		
		MapMirror mapMirror=null;
		
		Room room=null;
		
		while(it.hasNext()){
			
			mapMirror = (MapMirror)it.next();
			
			if(i>=(nowPage-1)*roomList.getSize() && i<(nowPage)*roomList.getSize()){
				
				room=new Room();
				
				room.setId(mapMirror.getId());
				
				room.setName(mapMirror.getName());
				
				room.setMaxNum(mapMirror.getNum());
				
				room.setNowNum(mapMirror.getPersonList().size());
				
				room.setLocked(mapMirror.isLocked());
				
				rooms.add(room);
				
			}
			
			if(i>=hashMapMirror.size() ||i>(nowPage)*roomList.getSize()){
								
				break;
				
			}
			
			i++;
			
		}
		
		roomList.setRoomList(rooms);
		
		//*********************************
		
		//roomList.setRoomList(roomList);
		
		JSONObject roomListJson=JSONObject.fromObject(roomList);
		
		return roomListJson.toString();
		
	}
	
	//根据id查找指定的
	
	public String findMapMirrorByIdString(Integer id){
		
		MapMirror mm=hashMapMirror.get(id);
		
		if(mm==null){
			
			return "";
			
		}else{
			
			JSONObject mapMirrorJson=JSONObject.fromObject(mm);
			
			return mapMirrorJson.toString();
			
		}
		
	}
	
	//根据id查找指定房间
	
	public MapMirror findMapMirrorByIdObject(Integer id){
		
		return hashMapMirror.get(id);
		
	}
	
	
}
